import 'package:shared_preferences/shared_preferences.dart';

class SPUtils {

  static SharedPreferences? _preferences;

  // 初始化方法，只需在应用启动时调用一次
  static Future<void> init() async {
    _preferences = await SharedPreferences.getInstance();
  }

  static set(String key, dynamic value) async {
    if (_preferences == null) {
      await init();  // 确保_preferences已初始化
    }
    // var prefer = await SharedPreferences.getInstance();
    if (value is String) {
      _preferences!.setString(key, value);
    } else if (value is int) {
      _preferences!.setInt(key, value);
    } else if (value is double) {
      _preferences!.setDouble(key, value);
    } else if (value is bool) {
      _preferences!.setBool(key, value);
    }
  }

  static get(String key) async {
    if (_preferences == null) {
      await init();  // 确保_preferences已初始化
    }
    // var prefer = await SharedPreferences.getInstance();
    var data = _preferences!.get(key);
    return data;
  }
}
